<template>
  <jy-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="分组名称" prop="groupName">
        <el-input v-model="form.groupName" placeholder="请输入试验任务预设分组名称" />
      </el-form-item>
      <el-form-item label="所属试验类型" prop="syType">
        <el-cascader v-model="form.syTypeId" disabled :options="syTypeList" :show-all-levels="false" :props="{ expandTrigger: 'hover', value: 'id', emitPath: false }" style="width: 100%;"></el-cascader>
      </el-form-item>
      <el-table border stripe ref="typeTable" v-loading="typeLoading" :data="biExcelTypeList" @selection-change="selectionChange" row-key="excelTypeId">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" type="index" width="50px" align="center" />
        <el-table-column label="试验名称" prop="excelTypeName" align="center" min-width="180" show-overflow-tooltip />
      </el-table>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="small" :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button size="small" @click="cancel">取 消</el-button>
    </div>
  </jy-dialog>
</template>
<script>
import { getBsReportGroup, addBsReportGroup, updateBsReportGroup } from "@/api/syrw/bsReportGroup";

export default {
  name: "bsReportGroupView",
  computed: {
    deptId: {
      get() {
        return this.$store.state.dept.deptId;
      }
    }
  },
  props: {
    syTypeList: {
      type: Array,
      default() {
        return [];
      }
    },
    syType: {
      type: [Number, String],
      default() {
        return "";
      }
    },
    biExcelTypeList: {
      type: Array,
      default() {
        return [];
      }
    }
  },
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      typeLoading: false,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        syTypeId: [{ required: true, message: "试验类型id不能为空", trigger: "change" }],
        groupName: [{ required: true, message: "试验任务预设分组名称不能为空", trigger: "blur" }]
      }
    };
  },
  created() {},
  mounted() {},
  methods: {
    initView(list) {
      this.reset();
      this.title = "预设分组保存";
      this.open = true;
      this.setSelection(list);
    },
    initAddOrUpdate(id) {
      this.reset();
      this.open = true;
      this.form.id = id;
      if (id) {
        this.title = "预设分组修改";
        this.getInfo();
      } else {
        this.title = "预设分组新增";
      }
    },
    getInfo() {
      getBsReportGroup(this.form.id).then((response) => {
        this.form = response.data;
        if (this.form.excelTypeIdList.length > 0) {
          this.setSelection(this.form.excelTypeIdList);
        }
      });
    },
    // 设置选中
    setSelection(list) {
      this.$nextTick(() => {
        const arr = [...list];
        this.biExcelTypeList.forEach((item) => {
          // 是否包含
          if (arr.includes(item.excelTypeId)) {
            this.$refs.typeTable.toggleRowSelection(item, true);
          }
        });
      });
    },
    // 多选框选中类型
    selectionChange(selection) {
      this.form.excelTypeIdList = selection.map((item) => item.excelTypeId);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.$refs.typeTable.clearSelection();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        deptId: this.deptId,
        syTypeId: this.syType,
        groupName: undefined
      };
      this.resetForm("form");
    },
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateBsReportGroup(this.form)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.cancel();
                this.$emit("callBack");
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addBsReportGroup(this.form)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.cancel();
                this.$emit("callBack");
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
</style>
